/*
 * @lc app=leetcode.cn id=1399 lang=typescript
 *
 * [1399] 统计最大组的数目
 */

// @lc code=start
function countLargestGroup(n: number): number {
    const countSum = (num: number) => {
        let count = 0;
        while (num) {
            count += num % 10;
            num = Math.floor(num / 10);
        }
        return count;
    };
    let cache = new Map<number, number[]>();
    for (let i = 1; i < n + 1; i++) {
        const index = countSum(i);
        cache.set(index, [...(cache.get(index) || []), i]);
    }
    const values = [...cache.values()];
    const maxNum = Math.max(...values.map((i) => i.length));
    return values.filter((i) => {
        return i.length === maxNum;
    }).length;
}
// @lc code=end
